$(function() {
	var userInfo = getLocalUserInfo();
    $.sValid.init({
        rules: {
            username: "required",
            userpwd: "required"
        },
        messages: {
            username: "用户名必须填写！",
            userpwd: "密码必填!"
        },
        callback: function(e, r, a) {
            if (e.length > 0) {
                var i = "";
                $.map(r,function(e, r) {i += "<p>" + e + "</p>"});
                layer.open({content: i, skin: 'msg', time: 2});
            }
        }
    });
    var a = true;
    $("#loginbtn").on('tap',function() {
        var username = $("#username").val();
        var userpwd = $("#userpwd").val();
        if ($.sValid()) {
            $.ajax({
                type: "post",
                url: ApiUrl,
                contentType: 'application/json',
                data:JSON.stringify({
                    "op": "Login",
                    "Login": username,
                    "PassWord": userpwd,
    			}),
                dataType: "json",
                success: function(e) {
                	console.log(e);
                    if (e.status == 0) {
			          layer.open({content: e.Message, skin: 'msg', time: 2});
			            return;
                    } else {
                         userInfo = e;
                         console.log(JSON.stringify(e));
                         localStorage.setItem('user_info', JSON.stringify(e.Main));
                        console.log(JSON.stringify(localCache('login_prev_page')));
                        var page = localCache('login_prev_page');
                        console.log(page.page_id);
                        pageChange('../'+ page.page_id+'.html');
                        localCache('login_prev_page', null, -1);
                    }
                }
            })
        }
    });
        mui.plusReady(function () {
        var tuichu = "再按一次退出程序";
        var backButtonPress = 0;

        // 关闭所有无用窗口
        clearAllWindows();
        mui.back = function(event) {
            backButtonPress++;
            if(backButtonPress > 1) {
                var main = plus.android.runtimeMainActivity();
                main.moveTaskToBack(false);
                return false;
            } else {
                plus.nativeUI.toast(tuichu);
            }
            setTimeout(function() {
                backButtonPress = 0;
            }, 1000);
            return false;
        };
       });
});